package com.cs456.TeamBobcat.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class MyDBhelper extends SQLiteOpenHelper{
    private static final String CREATE_TABLE="create table "+
    Constants.TABLE_NAME+" ("+
    Constants.KEY_ID+" integer primary key autoincrement, "+
    Constants.TITLE_NAME+" text not null, "+
    Constants.CONTENT_NAME+" text not null, "+
    Constants.DATE_NAME+" long);";
    private static final String CREATE_GPS_TABLE="create table "+
    Constants.GPS_NAME+" ("+
    Constants.KEY_ID+" integer primary key autoincrement, "+
    Constants.GPS_X+" double, "+
    Constants.GPS_Y+" double," +
    Constants.DATE_NAME+" long);";
    private static final String CREATE_WDTS_TABLE="create table "+ 
    Constants.WDTS_NAME+" ("+
    Constants.KEY_ID+" integer primary key autoincrement, "+
    Constants.WDTS_TEXT+" text not null, "+
    Constants.CONTENT_NAME+" text not null, "+
    Constants.DATE_NAME+" long);";
    private static final String CREATE_AUDIO_TABLE="create table "+ 
    Constants.AUDIO_TABLE_NAME+" ("+
    Constants.KEY_ID+" integer primary key autoincrement, "+
    Constants.AUDIO_PATH+" text not null, "+
    Constants.AUDIO_TEXT+" text not null, "+
    Constants.DATE_NAME+" long);";
    public MyDBhelper(Context context, String name, CursorFactory factory,
                      int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.v("MyDBhelper onCreate","Creating all the tables");
        try {
            db.execSQL(CREATE_TABLE);
            db.execSQL(CREATE_GPS_TABLE);
            db.execSQL(CREATE_WDTS_TABLE);
            db.execSQL(CREATE_AUDIO_TABLE);
        } catch(SQLiteException ex) {
            Log.v("Create table exception", ex.getMessage());
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion,
                          int newVersion) {
        Log.w("TaskDBAdapter", "Upgrading from version "+oldVersion
                               +" to "+newVersion
                               +", which will destroy all old data");
        db.execSQL("drop table if exists "+Constants.TABLE_NAME);
        db.execSQL("drop table if exists "+Constants.GPS_NAME);
        db.execSQL("drop table if exists "+Constants.WDTS_NAME);
        db.execSQL("drop table if exists "+Constants.AUDIO_TABLE_NAME);
        onCreate(db);
    }
}